
<template>
  <MainSlot v-loading="loading">
    <template v-slot:searchForm>
      <SearchForm v-if="formItemList.length" :formItemList="formItemList" :unfoldFlag="unfoldFlag" @search="search"
        @isUnfold="(v) => { unfoldFlag = v }">
      </SearchForm>
    </template>
    <template v-slot:table>
      <TableData  :tableThead="tableThead" :dataList="dataList" :loading="loading">
        <template v-slot:info="{ row }">
          <el-button type="text" @click="lookView(row)">查看详情</el-button>
        </template>
      </TableData>
    </template>
    <template v-slot:pagination>
      <Pagination @size-Change="handleSizeChange" @current-change="handleCurrentChange" :size="size" :page="page" :total="total">
      </Pagination>
    </template>
    <template v-slot:dialogModule>
      <el-dialog :close-on-click-modal="false"  title="估产详情" :visible.sync="dialogVisible_lookView" width="60%" :before-close="handleClose">
        <el-table :data="predictYieldDetailList" style="width: 100%" border>
          <el-table-column prop="plantVarietyName" label="估产品种" width="180">
          </el-table-column>
          <el-table-column prop="specificationName" label="估产规格" width="180">
          </el-table-column>
          <el-table-column prop="num" label="估产重量(粒)">
          </el-table-column>
          <el-table-column prop="weight" label="估产重量(斤)">
          </el-table-column>
        </el-table>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible_lookView = false">取 消</el-button>
          <el-button type="primary" @click="dialogVisible_lookView = false">确 定</el-button>
        </span>
      </el-dialog>
    </template>
  </MainSlot>
</template>
<script>
import MainSlot from "@/components/MainSlot";
import Pagination from '@/components/Pagination';
import SearchForm from '@/components/SearchForm';
import TableData from '@/components/TableData';
import BtnSlot from '@/components/BtnSlot';
import traditionAssessManagementCT from '@/api/assessManagement/traditionAssessManagementCT.js';
import specificationManagement from '@/api/basicInformation/specificationManagement.js';
import facilityManagement from '@/api/digtalOperation/facilityManagement.js';
import varietyManagement from '@/api/basicInformation/varietyManagement.js';
import batch from '@/api/batch/batchManagement';


export default {
  components: { MainSlot, Pagination, SearchForm, TableData, BtnSlot },
  data() {
    return {
      predictYieldDetailList: [],
      dialogVisible_lookView: false,
      unfoldFlag: false,
      loading: false,
      dialogVisible: false,
      latLngList: [],
      btnList: ['add', 'delete'],
      addEditFlag: 'add',
      formData: {},
      page: 1,
      size:10,
      total: 100,
      selectIndex: 0,
      searchFormData: {},
      dataList: [],
      tableThead: [
        { isShow: true, label: '养殖设施', prop: 'facilityName' },
        { isShow: true, label: '估产批次', prop: 'batchName' },
        { isShow: true, label: '估产组数(组)', prop: 'batchNum' },
        { isShow: true, label: '估产详情', slotName: 'info' },
        { isShow: true, label: '估产人', prop: 'staffName' },
        { isShow: true, label: '估产日期', prop: 'samplingDate' }
      ],
      formItemList: [
        {
          label: '设施名称',
          type: 'select',
          selectOptions: [],
          param: 'facilityId',
          defaultSelect: '', // 下拉框默认选中项
          multiple: false,
          isShow: true
        },
        {
          label: '批次名称',
          type: 'select',
          selectOptions: [],
          param: 'batchId',
          defaultSelect: '', // 下拉框默认选中项
          multiple: false,
          isShow: true
        },
        {
          label: '操作人',
          type: 'select',
          selectOptions: [],
          param: 'staffName',
          defaultSelect: '', // 下拉框默认选中项
          multiple: false,
          isShow: true
        },
        {
          label: '估产日期',
          type: 'datetimerange',
          param: 'dateTime',
          multiple: false,
          isShow: true
        },
      ],
      varietyList: [],
      facilityList: [],
      batchList: [],
      specificationList: []
    }
  },
  computed: {
    person() {
      return this.$store.state.initData.person || [];
    }
  },
  mounted() {
    this.queryPageByCondition();
    this.initData();
  },

  methods: {
    async initData() {
      let { data: specificationList } = await specificationManagement.queryByCondition({});
      this.specificationList = specificationList;
      // 获取批次列表 
      let { data: batchList } = await batch.queryByCondition({ batchType: 'BREEDING' })
      this.batchList = batchList
      //获取品种；
      let { data: varietyList } = await varietyManagement.queryByCondition({})
      this.varietyList = varietyList || [];
      //获取养殖设施；
      let { data: facilityList } = await facilityManagement.getFacilityListByConditionAll({
        breedType: sessionStorage.breedType
      });
      this.facilityList = facilityList || [];
      this.setFormOptions();
    },
    setFormOptions() {
      let list = JSON.parse(JSON.stringify(this.formItemList));
      this.formItemList = []
      list.forEach((item, index) => {
        if (item.param.indexOf('facilityId') !== -1) {
          console.log(this.facilityList)
          let selectOptions = this.facilityList.map(data => {
            return {
              text: data.facilityName, value: data.facilityId
            }
          })
          item.defaultSelect = this.searchFormData[item.param];
          item.selectOptions = selectOptions;
        };
        if (item.param.indexOf('batchId') !== -1) {
          let selectOptions = this.batchList.map(data => {
            return {
              text: data.batchName, value: data.batchId
            }
          })
          item.defaultSelect = this.searchFormData[item.param];
          item.selectOptions = selectOptions;
        };
        if (item.param == 'staffName') {
          let selectOptions = this.person.map(data => {
            return {
              text: data.person.name, value: data.person.name
            }
          })
          item.defaultSelect = this.searchFormData[item.param]
          item.selectOptions = selectOptions;
        };
      });
      this.$nextTick(() => {
        this.$set(this, 'formItemList', list)
        console.log(this.formItemList)
      })
    },
    queryPageByCondition() {
      let params = {
        page: this.page,
        size: this.size,
        entity: {
          ...this.searchFormData
        }
      }
      traditionAssessManagementCT.queryPageByCondition(params).then(res => {
        if (res.code == 1) {
          this.dataList = res.data.content || [];
          this.total = res.data.totalElements;
          if (res.data.totalPages && this.page > res.data.totalPages) {
            this.page = res.data.totalPages;
            this.queryPageByCondition();
          }
        } else {
          this.$message.warning(res.msg || '分页查询失败')
        }
      }).catch(err => {
        console.log(err)
      })
    },
    search(data) {
      this.page = 1;
      let endData = null;
      let startData = null;
      if (data.dateTime && data.dateTime.length == 2) {
        startData = `${data.dateTime[0]} 00:00:00`;
        endData = `${data.dateTime[1]} 23:59:59`;
      }
      this.searchFormData = {
        ...this.searchFormData,
        ...data,
        endTime: endData,
        startTime: startData
      };
      this.queryPageByCondition();
    },
    handleCurrentChange(page) {
      this.page = page;
      this.queryPageByCondition();
    },
    
    handleSizeChange(size){
      this.size = size;
      this.queryPageByCondition();
    },
    lookView(row) {
      this.predictYieldDetailList = row.predictYieldDetailList || [];
      if (this.predictYieldDetailList.length == 0) {
        this.$message.warning('暂无抽检详情')
      }else{
        this.dialogVisible_lookView = true;
      }
    },
    //获取选中Btn
    getBtnEvent(flag) {
      console.log(flag)
      if (flag == 'add') {
        this.dialogVisible = true
      } else if (flag == 'delete') {
        if (this.selectList.length > 0) {
          this.delList(this.selectList)
        } else {
          this.$message.warning('请先选择表单数据项后再删除！')
        }
      }
      this.addEditFlag = flag
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => { });
    }
  }
};
</script>
<style lang="scss" scoped>
.pointer {
  cursor: pointer;
}
</style>
  
  